﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using AMS.UI.Controls;
using AMS.ComponentModel;
using System.Threading;

namespace AMS.UI.Forms
{
    /// <summary>
    /// 窗体的基类
    /// </summary>
    public partial class BaseForm : DevComponents.DotNetBar.Office2007Form, IWorkForm
    {
        /// <summary>
        /// 窗体的基类
        /// </summary>
        public BaseForm()
        {
            InitializeComponent();
        }

        private IInstantHelpProvider instantHelperProvider;
        /// <summary>
        /// 即时帮助提供接口
        /// </summary>
        public IInstantHelpProvider InstantHelperProvider
        {
            get { return instantHelperProvider; }
            set { instantHelperProvider = value; }
        }

        private App.DelayLoading delayLoading;

        /// <summary>
        /// 初始化函数
        /// </summary>
        public virtual void Init()
        {
        }

        /// <summary>
        /// 启用延迟加载，使用默认的延迟时间(100ms)
        /// </summary>
        protected void ApplyDelayLoadData()
        {
            ApplyDelayLoadData(100);
        }

        /// <summary>
        /// 延时加载数据
        /// </summary>
        /// <param name="delayMiliseconds"></param>
        protected void ApplyDelayLoadData(int delayMiliseconds)
        {
           delayLoading = new App.DelayLoading(this, delayMiliseconds, new MethodInvoker(DelayLoadData));
           delayLoading.Execute();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        protected virtual void DelayLoadData()
        {
        }

        /// <summary>
        /// 是否可以关闭
        /// </summary>
        /// <returns></returns>
        public virtual bool CanClose()
        {
            return true;
        }

        /// <summary>
        /// 窗体的标识
        /// </summary>
        public virtual string Identity
        {
            get { return null; }
        }

        private bool isDialog;
        /// <summary>
        /// 标识是否对话框
        /// </summary>
        [DefaultValue(false)]
        public bool IsDialog
        {
            get { return isDialog; }
            set
            {
                isDialog = value;
                ShowInTaskbar = !isDialog;
                // TODO: 是否需要还需要斟酌
                // FormBorderStyle = isDialog ? FormBorderStyle.FixedDialog : System.Windows.Forms.FormBorderStyle.Sizable;
                StartPosition = isDialog ? FormStartPosition.CenterParent : FormStartPosition.Manual;
            }
        }

        /// <summary>
        /// 将子控件添加到即时消息显示对象中
        /// </summary>
        /// <param name="recursive"></param>
        /// <param name="controls"></param>
        public virtual void AddInstantHelperToControls(bool recursive, params Control[] controls)
        {
            if (controls == null || controls.Length == 0)
            {
                return;
            }

            foreach (Control ctrl in controls)
            {
                if (ctrl is GroupBox || ctrl is Panel || ctrl is UserControl || ctrl is Form)
                {
                    if (recursive)
                    {
                        List<Control> children = new List<Control>();
                        foreach (Control child in ctrl.Controls)
                        {
                            children.Add(child);
                        }

                        AddInstantHelperToControls(recursive, children.ToArray());
                    }
                }
                else
                {
                    ctrl.GotFocus += new EventHandler(ctrl_GotFocus);
                    ctrl.LostFocus += new EventHandler(ctrl_LostFocus);
                }
            }
        }

        /// <summary>
        /// 将控件从即时消息显示中移除
        /// </summary>
        /// <param name="controls"></param>
        public virtual void RemoveInstantHelpFromControls(params Control[] controls)
        {
            if (controls == null || controls.Length == 0)
            {
                return;
            }

            foreach (Control ctrl in controls)
            {
                if (ctrl.CanFocus)
                {
                    ctrl.GotFocus -= new EventHandler(ctrl_GotFocus);
                    ctrl.LostFocus -= new EventHandler(ctrl_LostFocus);
                }
            }
        }

        /// <summary>
        /// 即时消息控件获取焦点时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void ctrl_GotFocus(object sender, EventArgs e)
        {
            if (InstantHelperProvider != null)
            {
                instantHelperProvider.ShowHelper(Identity, (sender as Control).Name);
            }
        }

        void ctrl_LostFocus(object sender, EventArgs e)
        {
            if (InstantHelperProvider != null)
            {
                instantHelperProvider.HideHelper(Identity, (sender as Control).Name);
            }
        }

        #region 与提示框相关的API
        private DevComponents.DotNetBar.Balloon balloonCtrl;
        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="text"></param>
        /// <param name="ctrl"></param>
        public void balloon(string text, Control ctrl)
        {
            balloon(string.Empty, text, ctrl);
        }
        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="Caption"></param>
        /// <param name="Text"></param>
        /// <param name="ctrl"></param>
        public void balloon(string Caption, string Text, Control ctrl)
        {
            balloon(Caption, Text, ctrl, -1);
        }
        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="Caption"></param>
        /// <param name="Text"></param>
        /// <param name="ctrl"></param>
        /// <param name="Height"></param>
        public void balloon(string Caption, string Text, Control ctrl, int Height)
        {
            balloon(Caption, Text, ctrl, Height, -1);
        }

        /// <summary>
        /// 显示提示框。
        /// </summary>
        /// <param name="Caption"></param>
        /// <param name="Text"></param>
        /// <param name="ctrl"></param>
        /// <param name="Height"></param>
        /// <param name="waitSeconds">如果为-1则使用默认的时间</param>
        public void balloon(string Caption, string Text, Control ctrl, int Height, int waitSeconds)
        {
            if (balloonCtrl != null && !balloonCtrl.IsDisposed)
            {
                balloonCtrl.Hide();
            }
            else
            {
                balloonCtrl = new DevComponents.DotNetBar.Balloon();
            }

            if (waitSeconds < 0)
            {
                // 使用默认的等待时间
                waitSeconds = 3;
            }

            balloonCtrl.Style = DevComponents.DotNetBar.eBallonStyle.Balloon;
            balloonCtrl.AlertAnimation = DevComponents.DotNetBar.eAlertAnimation.TopToBottom;
            balloonCtrl.AutoScroll = true;
            balloonCtrl.AutoClose = true;
            balloonCtrl.AutoCloseTimeOut = waitSeconds;
            balloonCtrl.Owner = this;
            balloonCtrl.CaptionText = Caption;
            balloonCtrl.Text = Text;
            balloonCtrl.Width = 200;
            if (Height > 0)
            {
                balloonCtrl.Height = Height;
            }

            balloonCtrl.Show(ctrl, true);
            ctrl.Focus();
        }

        /// <summary>
        /// 关闭显示提示框
        /// </summary>
        public void CloseBaloon()
        {
            if (balloonCtrl != null)
            {
                balloonCtrl.Close();
            }
        }
        #endregion

        protected override void OnClosed(EventArgs e)
        {
            if (delayLoading != null)
            {
                delayLoading.Dispose();
                delayLoading = null;
            }

            base.OnClosed(e);
        }
    }
}
